<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 6/30/17
 * Time: 3:01 PM
 */
declare(strict_types=1);


namespace application\module\admin\addon\menu\model;

use application\module\admin\addon\model\AddonModel;

/**
 * Class MenuModel 菜单模型
 *
 * @property string $name
 * @property string $icon
 * @property string $value
 *
 * @property int $orderNo
 * @property int $pid
 * @property int $category
 *
 * @property string $target
 * @property string $update_time
 *
 * @property int $disable
 * @package application\module\admin\addon\menu\model
 */
class MenuModel extends AddonModel
{
    protected function tableName()
    {
        return 'menu';
    }

    public function save($justInsert = false): bool
    {
        $this->disable = $this->disable ? 1 : 0;
        return parent::save($justInsert);
    }

    public function install()
    {
        parent::install(); // TODO: Change the autogenerated stub
        $table = $this->tablePrefix() . $this->tableName();
        $this->exec("
INSERT INTO `{$table}` (`id`, `name`, `icon`, `value`, `orderNo`, `pid`, `category`, `update_time`, `disable`, `target`) VALUES
(1,'管理页面','ravelry','',0,0,0,'2017-05-12 19:05:46',0,'_self'),
(2,'菜单','list-ul','admin/menu/index',110,1,0,'2017-05-12 19:06:05',0,'_self'),
(3,'账户权限管理','lock','',90,1,0,'2017-05-12 19:08:13',0,'_self'),
(4,'权限','link','admin/access/index',700,3,0,'2017-05-12 19:09:47',0,'_self'),
(6,'角色','user-circle-o','admin/role/index',800,3,0,'2017-05-12 19:13:02',0,'_self'),
(7,'反射','map-o','admin/reflect/index',900,3,0,'2017-05-12 19:17:48',0,'_self'),
(8,'可视化','windows','admin/index/authexplorer',0,1,0,'2017-05-12 19:41:32',1,'_self'),
(9,'用户管理','user-o','admin/user/index',100,90,0,'2017-05-12 22:58:04',0,'_self'),
(10,'用户角色','lock','admin/auth/userrole',600,3,0,'2017-05-15 00:15:13',0,'_self'),
(11,'角色权限','lock','admin/auth/roleaccess',500,3,0,'2017-05-15 00:15:36',0,'_self'),
(12,'数据库管理','database','admin/database/index',0,1,0,'2017-05-16 00:43:48',0,'_self'),
(13,'日程','calendar','admin/calendar/index',0,1,0,'2017-05-16 01:00:15',0,'_self'),
(14,'系统参数','product-hunt','admin/property/index',0,1,0,'2017-05-16 01:01:19',0,'_self'),
(26,'Window','windows','admin/index/authexplorer',0,1,0,'2017-05-18 17:39:37',0,'_self'),
(36,'助手','ravelry','',0,1,0,'2017-05-19 17:34:47',0,'_self'),
(37,'图标','ravelry','admin/helper/icon',0,36,0,'2017-05-19 17:35:03',0,'_self'),
(38,'添加成员','user-o','admin/manager/dingbind',5000,27,0,'2017-05-24 19:52:02',1,'_self')");
    }

    /**
     * 获取菜单列表
     * @param int $category
     * @return array
     */
    public function getlist(int $category = 0): array
    {
        $query = $this->fields('nm.*,IFNULL(naa.id ,0)as aid')->alias('nm')
            ->leftOuterJoin(' {{auth_access}} naa on nm.`value` = naa.`value`');
        $list = $query->where(['nm.category' => $category, 'nm.disable' => 0])->order('nm.orderNo desc')->select();
        return $list;
    }

    protected function struct(): string
    {
        return "
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT 'untitled' COMMENT '菜单项名称',
  `icon` varchar(255) NOT NULL DEFAULT '' COMMENT 'fa图标',
  `value` varchar(255) NOT NULL DEFAULT '' COMMENT '菜单项值',
  `orderNo` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '排序',
  `pid` int(255) unsigned NOT NULL DEFAULT '0' COMMENT '为0时表示顶级菜单，否则表示特定的菜单项的子菜单项',
  `category` tinyint(255) unsigned NOT NULL DEFAULT '1' COMMENT '菜单类型，类型定义在menu_category中实现',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `disable` tinyint(4) unsigned NOT NULL DEFAULT '0' COMMENT '是否禁用',
  `target` varchar(16) CHARACTER SET ascii NOT NULL DEFAULT '_self' COMMENT 'a标签的target属性',
  PRIMARY KEY (`id`)
  ";
    }

}